const router = require('express').Router();
const fs = require('fs');
const db = require('../MySQLdb/dbQuery');
const { encode, decode } = require('../utils/token.js');
const upload = require('../utils/upload.js');
// 用户注册
router.post('/register', (req, res) => {
  let { username: user, password: pwd, name } = req.body;
  let sql = `select * from Users where username='${user}'`;
  db.query(sql, [], (status, data) => {
    if (!status) {
      return res.send(data);
    } else if (data.data.length) {
      data.data = '该账号已被注册';
      return res.send(data);
    } else {
      let sql = 'insert into Users(username,pwd,name)values(?,?,?);';
      let params = [user, pwd, name];
      db.query(sql, params, (status, data) => {
        if (!status) {
          return res.send(data);
        } else {
          data.data = '注册成功';
          return res.send(data);
        }
      });
    }
  });
});
// 用户登录
router.post('/login', (req, res) => {
  let { username: user, password: pwd } = req.body;
  let sql = `select * from Users where username='${user}';`;
  db.query(sql, [], (status, data) => {
    if (!status) {
      return res.send(data);
    } else if (!data.data.length) {
      data.msg = '未注册，请先注册';
      return res.send(data);
    } else {
      let sql = `select * from Users where username='${user}' and pwd='${pwd}';`;
      db.query(sql, [], (status, data) => {
        if (!status) {
          return res.send(data);
        } else if (!data.data.length) {
          data.msg = '密码错误';
          return res.send(data);
        } else {
          data.msg = '登录成功';
          data.token = encode({ id: data.data[0].id }, 'TODOkey');
          return res.send(data);
        }
      });
    }
  });
});
// 修改用户名
router.get('/updatename', (req, res) => {
  let id = req.auth.id;
  let name = req.query.name;
  let sql = `update Users set name='${name}' where id=${id};`;
  db.query(sql, [], (static, data) => {
    if (!static) {
      return res.send(data);
    } else {
      data.data = '修改成功';
      return res.send(data);
    }
  });
});
// 修改密码
router.get('/updatepwd', (req, res) => {
  let id = req.auth.id;
  let pwd = req.query.password;
  let sql = `update Users set pwd='${pwd}' where id=${id};`;
  db.query(sql, [], (static, data) => {
    if (!static) {
      return res.send(data);
    } else {
      data.data = '修改成功';
      return res.send(data);
    }
  });
});
// 更换头像
router.get('/updateimg/:id', (req, res) => {
  return res.send({ msg: 'update' });
});
// 退出登录
router.get('/loginout', (req, res) => {
  return res.send({ msg: 'update' });
});
router.get('/getuser', (req, res) => {
  let uid = req.auth.id;
  let sql = 'select * from Users where id=?;';
  db.query(sql, [uid], (state, data) => {
    return res.send(data);
  });
});

router.post('/edituser', (req, res) => {
  let id = req.auth.id;
  let { name, oldpwd, newpwd } = req.body;
  let sql = 'select * from Users where id=? and pwd=?;';
  db.query(sql, [id, oldpwd], (status, data) => {
    if (!status) {
      return res.send(data);
    } else if (!data.data.length) {
      data.msg = '旧密码输入错误';
      return res.send(data);
    } else {
      let sql = 'update Users set name=?,pwd=? where id=?;';
      db.query(sql, [name, newpwd, id], (status, data) => {
        if (!status) {
          return res.send(data);
        } else {
          data.msg = '修改成功';
          return res.send(data);
        }
      });
    }
  });
});

router.post('/upload', upload.single('file'), (req, res) => {
  let filepath = '/images/' + req.file.filename;
  let id = req.auth.id;
  let sql = 'update Users set img=? where id=?;';
  db.query(sql, [filepath, id], (state, data) => {
    if (!state) {
      return res.send(data);
    } else {
      data.data = filepath;
      data.msg = '修改成功';
      return res.send(data);
    }
  });
});

router.get('/img', (req, res) => {
  let id = req.auth.id;
  let sql = 'select img from Users where id=?;';
  db.query(sql, [id], (state, data) => {
    if (!state) {
      return res.send(state);
    } else {
      let imgsrc = data.data[0].img;
      let rs = fs.createReadStream('../TODO/static' + imgsrc);
      rs.pipe(res);
    }
  });
});
module.exports = router;
